<?php
// @formatter:off
/**
 * @file c.comic.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-08-22 23:54:04 +0000 (Thu, 22 Aug 2013) $
 *
 * $Id: c.comic.php 103 2013-08-22 23:54:04Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/themes/default/controlers/contents/c.comic.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_COMIC_VIEW)) {
	require_once "{$this->_themeRoot}/controlers/generics/comicpreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wctDefaults;

	if($wcComic && $wcComic->ok()) {
		$wcComic->expand();

		$wcThemeAssigns["PATH"] = "{$wcComic->directory()->name}/{$wcComic->name}";
		$wcThemeAssigns["ACTIONPRESCRIPT"] = "/contents/c.comic.js";

		if(isset($_REQUEST["astable"])) {
			$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.comic.table.html";

			$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
			$minImgInPage = $wctDefaults["max-per-page"]["comic-astable"] * ($page - 1);
			$maxImgInPage = $minImgInPage + $wctDefaults["max-per-page"]["comic-astable"];

			$wcThemeAssigns["PATH"] .= " (page {$page})";

			$paramsNoImg = requestParams(true, $wcAction, array(
				"image",
				"astable"
			));

			$images = array();
			$imgCount = 0;
			foreach($wcComic->images() as $img) {
				if($imgCount >= $minImgInPage && $imgCount < $maxImgInPage) {
					$guess = $wcComic->previewUriFor($imgCount, false);
					$aux = array(
						"title" => $img->name,
						"link" => "{$paramsNoImg}&image={$imgCount}",
						"img" => $guess !== false ? $guess : $wcDefaults["preview"]
					);
					$images[] = $aux;
				}
				$imgCount++;
			}

			$wcThemeAssigns["images"] = $images;

			wcSetPager(requestParams(true, $wcAction, array("image")), ceil($imgCount / $wctDefaults["max-per-page"]["comic-astable"]), $page);
		} else {
			$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.comic.html";

			$paramsNoImg = requestParams(true, $wcAction, array("image"));
			$index = isset($_REQUEST["image"]) ? $_REQUEST["image"] : 0;
			$files = $wcComic->images();
			$imgCount = count($files);

			$wcThemeAssigns["PATH"] .= " (".($index + 1)."/{$imgCount})";

			$first = 0;
			$previous5 = $index - 5;
			$previous = $index - 1;
			$current = $index;
			$next = $index + 1;
			$next5 = $index + 5;
			$last = $imgCount - 1;

			if($previous5 < $first) {
				$previous5 = $last + $previous5;
				if($previous5 < $first) {
					$previous5 = $first;
				}
			}
			if($previous < $first) {
				$previous = $last;
			}
			if($next > $last) {
				$next = $first;
			}
			if($next5 > $last) {
				$next5 = $first + ($next5 - $last);
				if($next5 > $last) {
					$next5 = $last;
				}
			}

			$files[$first]->guess = $wcComic->previewUriFor($first, false);
			$files[$previous5]->guess = $wcComic->previewUriFor($previous5, false);
			$files[$previous]->guess = $wcComic->previewUriFor($previous, false);
			$files[$current]->guess = $wcComic->previewUriFor($current, false);
			$files[$next]->guess = $wcComic->previewUriFor($next, false);
			$files[$next5]->guess = $wcComic->previewUriFor($next5, false);
			$files[$last]->guess = $wcComic->previewUriFor($last, false);

			if($files[$first]->guess !== false) {
				$wcThemeAssigns["IMGFIRST"] = $files[$first]->guess;
			} else {
				$wcThemeAssigns["IMGFIRST"] = $wcDefaults["preview"];
			}
			if($files[$previous5]->guess !== false) {
				$wcThemeAssigns["IMGPREV5"] = $files[$previous5]->guess;
			} else {
				$wcThemeAssigns["IMGPREV5"] = $wcDefaults["preview"];
			}
			if($files[$previous]->guess !== false) {
				$wcThemeAssigns["IMGPREV"] = $files[$previous]->guess;
			} else {
				$wcThemeAssigns["IMGPREV"] = $wcDefaults["preview"];
			}
			if($files[$current]->guess !== false) {
				$wcThemeAssigns["IMGTABLE"] = $files[$current]->guess;
			} else {
				$wcThemeAssigns["IMGTABLE"] = $wcDefaults["preview"];
			}
			if($files[$next]->guess !== false) {
				$wcThemeAssigns["IMGNEXT"] = $files[$next]->guess;
			} else {
				$wcThemeAssigns["IMGNEXT"] = $wcDefaults["preview"];
			}
			if($files[$next5]->guess !== false) {
				$wcThemeAssigns["IMGNEXT5"] = $files[$next5]->guess;
			} else {
				$wcThemeAssigns["IMGNEXT5"] = $wcDefaults["preview"];
			}
			if($files[$last]->guess !== false) {
				$wcThemeAssigns["IMGLAST"] = $files[$last]->guess;
			} else {
				$wcThemeAssigns["IMGLAST"] = $wcDefaults["preview"];
			}

			$wcThemeAssigns["LNKFIRST"] = "{$paramsNoImg}&image={$first}";
			$wcThemeAssigns["LNKPREV5"] = "{$paramsNoImg}&image={$previous5}";
			$wcThemeAssigns["LNKPREV"] = "{$paramsNoImg}&image={$previous}";

			$wcThemeAssigns["LNKTABLE"] = "{$paramsNoImg}&astable&page=".ceil(($index + 1) / $wctDefaults["max-per-page"]["comic-astable"]);

			$wcThemeAssigns["LNKNEXT"] = "{$paramsNoImg}&image={$next}";
			$wcThemeAssigns["LNKNEXT5"] = "{$paramsNoImg}&image={$next5}";
			$wcThemeAssigns["LNKLAST"] = "{$paramsNoImg}&image={$last}";

			$wcThemeAssigns["MAINIMG"] = turlencode("{$wcComic->expandedUri()}{$files[$current]->lpath}");
			$wcThemeAssigns["INFO-IMG-NAME"] = basename($files[$current]->lpath);
			$wcThemeAssigns["INFO-NUMBER"] = $index + 1;
			$wcThemeAssigns["INFO-NUMBER-OF"] = $imgCount;
		}

		//
		// Setting gallery info
		// @{
		$aux = wcGetComicPreviewAssigns($wcComic);
		$wcThemeAssigns = array_merge($wcThemeAssigns, $aux);

		if($wcProfile->allowedTo(WC_PERM_COMIC_RENAME)) {
			$wcThemeAssigns["INFO-NAME-EDITABLE"] = true;
		}
		if($wcProfile->allowedTo(WC_PERM_COMIC_SET_TAG)) {
			$wcThemeAssigns["INFO-TAGS-EDITABLE"] = true;
		}
		if($wcProfile->allowedTo(WC_PERM_COMIC_SET_COLLECTION)) {
			$wcThemeAssigns["INFO-COLL-EDITABLE"] = true;
		}
		$wcThemeAssigns["INFO-NAME"] = $wcComic->next_name ? $wcComic->next_name : $wcComic->name;
		$wcThemeAssigns["INFO-NAME-LINK"] = $wcComic->id;
		$wcThemeAssigns["INFO-DIRECTORY"] = $wcComic->directory()->name;
		if($wcProfile->granted(WC_PERM_DIRECTORY_VIEW)) {
			$wcThemeAssigns["INFO-DIRECTORY-LINK"] = $wcComic->directory;
		}
		if($wcCollection) {
			$wcThemeAssigns["INFO-COLLECTIONS"] = $wcCollection->collectionPath();
		}
		$wcThemeAssigns["INFO-TAGS"] = array();
		foreach($wcTagsHolder->tagsOf($wcComic, $wcGroup) as $tag) {
			$aux = array(
				"name" => $tag->name,
				"link" => $tag->id
			);

			if(in_array($tag->name, $wcSpecialTags)) {
				$aux["special"] = true;
			}

			$wcThemeAssigns["INFO-TAGS"][$tag->name] = $aux;
		}
		ksort($wcThemeAssigns["INFO-TAGS"]);
		// @}
	} else {
		$wcThemeAssigns["error-string"] = "Invalid comic";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to open comics";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>